package com.dawn.sinaoption.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dawn.sinaoption.service.SinaService;
import com.dawn.usermanage.bean.UserPreference;
import com.opensymphony.xwork2.ActionSupport;

@Controller("sinaOptionAction")
@Scope("prototype")
public class SinaOptionAction extends ActionSupport implements SessionAware, ServletRequestAware {

    private static final long serialVersionUID = -4809462863722754095L;
    private static Logger logger = Logger.getLogger(SinaOptionAction.class);

    private Map<String, Object> session;
    private HttpServletRequest request;
    private InputStream inputStream;

    @Autowired
    private SinaService sinaService;

    public String followAction() {
        String sinaId = session.get("sinaId").toString();
        Long userId = ((UserPreference) session.get("user")).getUserId();
        int tradeId = Integer.parseInt(request.getParameter("tradeId"));
        String accessToken = (String) session.get("accToken");
        String accessTokenSecret = (String) session.get("accTokenSecret");
        String result = sinaService.followSinaer(userId, sinaId, tradeId, accessToken, accessTokenSecret);
        this.inputStream = new ByteArrayInputStream(result.getBytes());
        logger.info("follow test");
        return SUCCESS;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
